const express = require('express');
const router = express.Router();
const sqlFn = require('../mysql')
const remove = require("loadsh/remove");
/**
 * @swagger
 * tags:
 *   name: 业务管理
 *   description: 业务相关的操作接口
 */
/**
 * @swagger
 * /product/add:
 *   post:
 *     tags: [业务管理]
 *     summary: 添加产品
 *     description: 添加一个新产品到数据库
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               name:
 *                 type: string
 *               type:
 *                 type: string
 *               attrs:
 *                 type: string
 *               mainPic:
 *                 type: string
 *               morePic:
 *                 type: string
 *               isOnShelf:
 *                 type: boolean
 *               price:
 *                 type: number
 *               descs:
 *                 type: string
 *     responses:
 *       200:
 *         description: 产品添加成功
 */
router.post('/add', (req, res, next) => {

    const sql = 'insert into product (name, type, attrs, mainPic, morePic, isOnShelf,price,descs) VALUES (?, ?, ?, ?, ?, ?,?,?)';
    const {name, type, attrs, mainPic, morePic, isOnShelf, price, descs} = req.body;
    console.log(req.body)
    sqlFn(sql, [name, type, attrs, mainPic, morePic, isOnShelf, price, descs], (err, data) => {
        if (err) {
            next(err)
            return
        }
        if (data.affectedRows) {
            res.send({code: 200, success: true})
        }
    })
})
/**
 * @swagger
 * /product/list:
 *   get:
 *     tags: [业务管理]
 *     summary: 获取产品列表
 *     description: 分页查询产品列表
 *     parameters:
 *       - in: query
 *         name: current
 *         schema:
 *           type: integer
 *         description: 当前页码
 *       - in: query
 *         name: pageSize
 *         schema:
 *           type: integer
 *         description: 每页数量
 *       - in: query
 *         name: name
 *         schema:
 *           type: string
 *         description: 按名称搜索
 *       - in: query
 *         name: type
 *         schema:
 *           type: string
 *         description: 按类型搜索
 *     responses:
 *       200:
 *         description: 返回产品列表数据
 */
router.get('/list', function (req, res, next) {
    const { query } = req;
    const { current = 1, pageSize = 3 } = query;
    const sql = `select COUNT(*) as num from product
                 where  ${req.query.name ? 'locate(?,name)>0' : 'name is not null'}
                   and ${req.query.type ? 'locate(?,type)>0' : 'type is not null'}`
    const arr=[]
    query.name?arr.push(query.name):null
    query.type?arr.push(query.type):null
    sqlFn(sql, arr, function (err, data) {
        if (err) {
            next(err)
            return
        }
        const total = data[0].num
        const sql = `select * from product  where  ${req.query.name ? 'locate(?,name)>0' : 'name is not null'}
                                              and ${req.query.type ? 'locate(?,type)>0' : 'type is not null'} limit ${pageSize * (current - 1)},${pageSize}`
        sqlFn(sql, arr, function (err, data) {
            if (err) {
                next(err)
                return
            }
            res.send({ code: '00000', records: data, pagination: { total, pageSize } })
        })
    })
})
/**
 * @swagger
 * /product/update:
 *   post:
 *     summary: 更新产品信息
 *     tags: [业务管理]
 *     description: 根据ID更新产品信息
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               id:
 *                 type: integer
 *               name:
 *                 type: string
 *               type:
 *                 type: string
 *               attrs:
 *                 type: string
 *               mainPic:
 *                 type: string
 *               morePic:
 *                 type: string
 *               isOnShelf:
 *                 type: boolean
 *               price:
 *                 type: number
 *               descs:
 *                 type: string
 *     responses:
 *       200:
 *         description: 产品更新成功
 */
router.post('/update', function (req, res, next) {
    const sql = 'update product set `name`=?,`type`=?,`attrs`=?,`mainPic`=?,`morePic`=? ,`isOnShelf`=?,`price`=?,`descs`=? where `id`=?'
    const { name, type, attrs, mainPic, morePic, isOnShelf,price,descs, id } = req.body
    sqlFn(sql, [name,  type, attrs, mainPic, morePic, isOnShelf,price,descs,  id], function (err, data) {
        if (err) {
            next(err)
            return
        }
        if (data.affectedRows) {
            res.send({ code: '00000', success: true })
        }
    })
})
/**
 * @swagger
 * /product/delete:
 *   post:
 *     tags: [业务管理]
 *     summary: 删除产品
 *     description: 根据产品ID删除产品
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               id:
 *                 type: integer
 *     responses:
 *       200:
 *         description: 删除成功
 */
router.post('/delete', function (req, res, next) {
    const sql = 'delete from product where `id`=?'
    sqlFn(sql, [req.body.id], function (err, data) {
        if (err) {
            next(err)
            return
        }
        if (data.affectedRows) {
            res.send({code: 200, success: true})
        }
    })
})
/**
 * @swagger
 * /relateMenus:
 *   get:
 *     tags: [业务管理]
 *     summary: 获取关联菜单
 *     description: 获取所有 isOfAdmin=2 的菜单，并返回树状结构。
 *     responses:
 *       200:
 *         description: 返回树状菜单数据
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   example: 200
 *                 records:
 *                   type: array
 *                   items:
 *                     type: object
 *                     properties:
 *                       id:
 *                         type: integer
 *                         example: 1
 *                       name:
 *                         type: string
 *                         example: "系统管理"
 *                       pid:
 *                         type: integer
 *                         example: -1
 *                       children:
 *                         type: array
 *                         items:
 *                           type: object
 */
router.get('/relateMenus', (req, res, next) => {
    const sql = 'select * from menu where `isOfAdmin`=2'
    sqlFn(sql, [], function (err, data) {
        if (err) {
            next(err)
            return
        }
        const getMenuTree = (menus = []) => {
            menus.map(item => {
                menus.map(menu => {
                    if (item.pid == menu.id) {
                        menu.children = menu.children || []
                        menu.children.push(item)
                    }
                })
            })
            remove(menus, (menu) => menu.pid != -1)
            return menus
        }
        res.send({code: 200, records: getMenuTree(data)})
    })
})
/**
 * @swagger
 * /updateRelateMenus:
 *   post:
 *     tags: [业务管理]
 *     summary: 更新用户关联菜单
 *     description: 根据用户 ID 更新其关联的菜单。
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               relatedMenus:
 *                 type: string
 *                 example: "[1,2,3]"
 *               id:
 *                 type: integer
 *                 example: 1001
 *     responses:
 *       200:
 *         description: 更新成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   example: 200
 *                 success:
 *                   type: boolean
 *                   example: true
 */
router.post('/updateRelateMenus', (req, res, next) => {
    const sql = 'update user set `relatedMenus`=? where `id`=?'
    const {relatedMenus, id} = req.body
    sqlFn(sql, [relatedMenus, id], function (err, data) {
        if (err) {
            next(err)
            return
        }
        if (data.affectedRows) {
            res.send({code: 200, success: true})
        }
    })
})
module.exports = router;